package com.redxiii.safesync;


import java.io.File;
import java.io.IOException;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PatternOptionBuilder;
import org.apache.commons.cli.PosixParser;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.redxiii.util.cli.CmdLineIO;

public class SyncHandlerCli extends SyncHandler {

	private static final Logger logger = LoggerFactory.getLogger(SyncHandlerCli.class); 
	
	@SuppressWarnings("static-access")
	public SyncHandlerCli(String[] args) {
		Options options = new Options();
		options.addOption("auto", false, "automatic");
		options.addOption("p", true, "Password");
		options.addOption("L", true, "Local folder");
		options.addOption("R", true, "Remote folder");
		options.addOption(OptionBuilder.withArgName("p").hasArg().withType(PatternOptionBuilder.STRING_VALUE).withDescription("password").create("p"));
		
		
		config = loadConfiguration();
		
		CommandLineParser parser = new PosixParser();
		try {
			CommandLine line = parser.parse(options, args);
			if (line.hasOption("p"))
				config.setProperty("password", line.getOptionObject('p'));
			if (line.hasOption("auto"))
				automatic = true;
			
		} catch (ParseException e) {
			logger.error("Parsing failed.  Reason: " + e.getMessage());
			HelpFormatter formatter = new HelpFormatter();
			formatter.printHelp("java", options);
		}
	}
	
	protected PropertiesConfiguration loadConfiguration() {
		try {
			File preferences = new File("preferences.properties");
			
			if (!preferences.exists())
				preferences.createNewFile();
			
			return new PropertiesConfiguration(preferences);
		} catch (ConfigurationException e) {
			logger.error("Fail creating configuration file",e);
		} catch (IOException e) {
			logger.error("Fail creating configuration file",e);
		}
		return null;
	}
	
	@Override
	protected void getConfiguration() {
		
		try {
			String local = config.getString("local");
			String remote = config.getString("remote");
			
			CmdLineIO io = new CmdLineIO();
			
			File localFolder;
			File remoteFolder;
			do {
				local = io.getString("Local Folder (" + local + ")");
				if (local == null || local.length() == 0)
					local = config.getString("local");
				
				localFolder = new File(local);
			} while(!localFolder.isDirectory());
			config.setProperty("local", local);
			
			do {
				remote = io.getString("Remote Folder (" + remote + ")");
				if (remote == null || remote.length() == 0)
					remote = config.getString("remote");
				
				remoteFolder = new File(remote);
			} while(!remoteFolder.isDirectory());
			config.setProperty("remote", remote);
			
			((PropertiesConfiguration)config).save();
			
			String password = io.getString("Password");
			config.setProperty("password", password);
			
		} catch (ConfigurationException e) {
			logger.error("Fail loading configuration file",e);
		} 
		
	}
	public static void main(String[] args) throws ConfigurationException, IOException {
		
		new SyncHandlerCli(args).sync();
		
		
	}
}
